<?php use_helper('Javascript') ?>

<script>
//every 3 seconds
setInterval(function() {
    if(IsGeoChanged())
    {
        observe();
    }
}, 3000);
function observe()
{
    jQuery.ajax({
        type: "post"
        , data: ({'mask': jQuery('#partner_geo_name_value').val() })
        , dataType: "html"
        , url: "/public_html/test.php/partners/getlist"
        , success: function(data) {
            jQuery('#partnerGeoList').html(data);
            jQuery("#partnerIndicator").hide();
          }
        , error: function() { console.debug("error"); }
        , beforeSend : function() { jQuery("#partnerIndicator").show(); }
    });
}
// To prevent loading by same mask
var gGeoMask = '';

function OnGeoClicked(id, name)
{
//    alert(name);
    jQuery('#partner_geo_name_id').val(id);
    jQuery('#partner_geo_name_value').val(name);
    jQuery('partnerGeoList').hide();

    // To prevent on change event
    gGeoMask = name;
}

function IsGeoChanged()
{
    // Check if city was not "changed" by clicking on autocomplete list
    var input = jQuery('#partner_geo_name_value').val();
    if( (input.lenght < 2) || (input == gGeoMask) )
    {
        return false;
    }

    return true;
}

</script>


<?php echo input_tag(
                'partner[geo_name_value]'
                , $partner->getGeoName()
                , array(
                    'autocomplete' => 'off'
                )
            ) ?>
<?php echo input_hidden_tag(
                'partner[geo_name_id]'
                , $partner->getGeoNameId()
) ?>
<div id="partnerGeoList">...</div>
<div id="partnerIndicator" style="display: none;"><img src="/images/ajaxloader.png" alt="Loading"></div>

<?php //echo observe_field(
//                'partner_geo_name_value'
//                , array(
//                    'url' => 'partners/getlist'
//                    , 'success' => "$('partnerGeoList').show();"
//                    , 'update' => 'partnerGeoList'
//                    , 'with' => "'mask=' + \$F('partner_geo_name_value')"
//                    , 'frequency' => '3'
//                    , 'loading' => 'Element.show("partnerIndicator")'
//                    , 'complete' => 'Element.hide("partnerIndicator")'
//                    , 'condition' => "IsGeoChanged()"
//                )) ?>
